package com.itheima.mp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.mapper.UserMapper;
import com.itheima.mp.service.IUserService;
import org.springframework.stereotype.Service;

/**
 * UserServiceImpl不想实现接口中的所有方法可以继承ServiceImpl<对应的mapper文件,需要操作的实体类>
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    /**
     * 根据id扣减money
     * @param id
     * @param money
     */
    @Override
    public void deductBalance(Long id, Integer money) {
        /**
         * 1.查询用户
         * 2.校验用户状态
         * 3.检查余额是否充足
         * 4.扣减金额
         */
        User user = getById(id);
        //校验
        if (user != null && user.getStatus() == 2) {
            throw new RuntimeException("用户状态异常");
        }
        //检查余额
        if (user.getBalance() < money){
            throw new RuntimeException("用户余额不足");
        }
        //扣减金额 update user set balance = balance - money
        baseMapper.deductBalance(id,money);
    }
}
